﻿// 开源小地图/ModifyMapLocationWidget.cs
using Engine;
using Game;
using System;
using System.Collections.Generic;

namespace Game
{
    public class ModifyMapLocationWidget : CanvasWidget
    {
        public RectangleWidget SmallSquare { get; private set; }

        public static Vector2? LastSavedPosition { get; set; }

        private ComponentPlayer m_componentPlayer;
        private bool m_isTouching;
        private Vector2? m_lastTouchPosition;
        private static Vector2? s_lastSavedPosition;

        public ModifyMapLocationWidget(ComponentPlayer componentPlayer)
        {
            this.m_componentPlayer = componentPlayer;
            InitializeWidget();
            SetupSmallSquare();
        }

        private void InitializeWidget()
        {
            base.Size = this.m_componentPlayer.GameWidget.Size;
            this.IsHitTestVisible = true;
            HandleParentWidgetVisibility(false);
        }

        private void HandleParentWidgetVisibility(bool isVisible)
        {
            if (base.ParentWidget != null)
                base.ParentWidget.IsHitTestVisible = isVisible;
        }

        private void SetupSmallSquare()
        {
            this.SmallSquare = this.m_componentPlayer.GuiWidget.Children.Find<RectangleWidget>("Smallsquare", true);
            Vector2 margin = LastSavedPosition ?? ComponentMap.InitialSquarePosition;
            if (this.SmallSquare != null)
                this.SmallSquare.Margin = margin;
        }

        public override void Update()
        {
            base.Update();
            ProcessInputActions();
        }

        private void ProcessInputActions()
        {
            WidgetInput input = this.m_componentPlayer.GameWidget.Input;
            Vector2? touchPosition = GetTouchPosition(input);

            var inputHandlers = new Dictionary<Func<bool>, Action>
            {
                { () => input.Tap != null && touchPosition != null, () => HandleTouchStart(touchPosition.Value) },
                { () => this.m_isTouching && touchPosition != null, () => UpdateSquarePosition(touchPosition.Value) },
                { () => input.Press == null && this.m_isTouching, () => HandleTouchEnd() }
            };

            foreach (var handler in inputHandlers)
            {
                if (handler.Key())
                    handler.Value();
            }
        }

        private Vector2? GetTouchPosition(WidgetInput input)
        {
            if (input.Tap != null)
                return new Vector2?(this.m_componentPlayer.GuiWidget.ScreenToWidget(input.Tap.Value));
            else if (input.Press != null)
                return new Vector2?(this.m_componentPlayer.GuiWidget.ScreenToWidget(input.Press.Value));
            return null;
        }

        private void HandleTouchStart(Vector2 position)
        {
            this.m_isTouching = true;
            UpdateSquarePosition(position);
        }

        private void HandleTouchEnd()
        {
            this.m_isTouching = false;
            CloseAndReopenSettings();
        }

        private void UpdateSquarePosition(Vector2 touchPos)
        {
            Vector2 vector = new Vector2(
                touchPos.X - this.SmallSquare.Size.X / 2f,
                touchPos.Y - this.SmallSquare.Size.Y / 2f
            );
            this.SmallSquare.Margin = vector;
            LastSavedPosition = vector;
        }

        private void CloseAndReopenSettings()
        {
            HandleParentWidgetVisibility(true);

            LastSavedPosition = this.SmallSquare?.Margin;
            ResetAndRecreateMapSettings();
        }

        private void ResetAndRecreateMapSettings()
        {
            var gui = this.m_componentPlayer.ComponentGui;
            gui.ModalPanelWidget = null;
            gui.ModalPanelWidget = new MapSettingsWidget(this.m_componentPlayer);
        }

        public override void Dispose()
        {
            HandleParentWidgetVisibility(true);
            base.Dispose();
        }
    }
}